/*****************************************************************************
* Author:		Wesley Maxey
* Information:  CST 238 Lab 2 GUI Basics
* Filename:		Lab_2.cpp
* Date Created:	4/10/2013
*
* Overview
*		
*
* Member Functions:
*public:
*       SuperString()
*       SuperString( const Tx ... )
*       void setString( const Tx ... )
*       char * getString()
*       ~SuperString()
*private:
*       void unwindArgs( const T, const Tx ... )
*       void unwindArgs()
*       void extractType() //Heavily overloaded function for deducing base type
*
* Contained Data:
*private:
*       char * m_str
*       int m_len
*
*****************************************************************************/
#define _CRT_SECURE_NO_WARNINGS

#include <Windows.h>
#include <mmsystem.h>
#include <iostream>
//This here is my cool contribution to Lab 1
#include "SuperString.h"
#include <time.h>

#define r ( ( rand() % 3 ) - 1 )

using std::strlen;

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
VOID CALLBACK Timer( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );

//Added a timer as a cool feature to Lab 2
UINT timer;

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst,
	PSTR lpszCmdParam, int nCmdShow ) {
		static char	szAppName[] = "Wesley Maxey - Lab 1";
		HWND		hwnd;
		MSG			msg;
		WNDCLASSEX	wndclass; 
		SuperString	temp("test ", 1, 2, 3, 4, 5 );

		srand( time(nullptr) );

		wndclass.cbSize			= sizeof(wndclass);
		wndclass.style			= CS_HREDRAW | CS_VREDRAW;
		wndclass.lpfnWndProc	= WndProc;
		wndclass.cbClsExtra		= 0;
		wndclass.cbWndExtra		= 0;
		wndclass.hInstance		= hInst;
		wndclass.hIcon			= LoadIcon (NULL, IDI_APPLICATION);

		wndclass.hCursor		= LoadCursor (NULL, IDC_ARROW);
		wndclass.hbrBackground	= WHITE_BRUSH;
		wndclass.lpszMenuName	= NULL;
		wndclass.lpszClassName	= szAppName;
		wndclass.hIconSm		= LoadIcon (NULL, IDI_APPLICATION);

		RegisterClassEx (&wndclass);


		hwnd = CreateWindow (szAppName,					// window class name
			"LAB 2 - Press Left or Right Mouse Button",	// window caption
			WS_OVERLAPPEDWINDOW,						// window style
			CW_USEDEFAULT,								// initial x position
			CW_USEDEFAULT,								// initial y position
			CW_USEDEFAULT,								// initial x size
			CW_USEDEFAULT,								// initial y size
			NULL,										// parent window handle
			NULL,										// window menu handle
			hInst,										// program instance handle
			NULL);										// creation parameters

		ShowWindow (hwnd, nCmdShow);
		UpdateWindow (hwnd);

		timer = SetTimer( hwnd,	// window handle
			0,					// id of the timer message, leave 0 in this case
			64,					// millis
			Timer );			// Callback

		while (GetMessage (&msg, NULL, 0, 0))
		{
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}
		return (int)(msg.wParam);
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	HDC				hdc;
	PAINTSTRUCT		ps;
	RECT			rect;
	SuperString		ss;				//Custom string class added as my special feature
	HBRUSH			hbrush = CreateSolidBrush( RGB(256,256,256) );
	int				len;
	RECT			t;
	static RECT		r1, r2;
	static RECT		r1_d, r2_d;
	static bool		lflag = false;	//WM_PAINT
	static bool		rflag = false;	//WM_ERASEBKGND
	GetClientRect(hwnd, &rect);
	t.bottom	= rect.bottom / 2;
	t.left		= rect.right / 2;
	t.right		= rect.right / 2;
	t.top		= rect.bottom / 2;

	switch (message)
	{
	case WM_KEYDOWN:
		if ( wParam == VK_F5 ) //If F5 was pressed play sound
			PlaySound( "sfile.wav", NULL, SND_SYNC | SND_FILENAME );
		break;
	case WM_LBUTTONDOWN:
		MessageBox( hwnd, "LMB clicked and then released", "LMB Pressed", MB_OK );
		lflag = !lflag;
		return 0;

	case WM_RBUTTONDOWN:
		MessageBox( hwnd, "RMB clicked and then released", "RMB Pressed", MB_OK );
		rflag = !rflag;
		return 0;

	case WM_SIZE:	//Reorients that graphics
		t.bottom	= rect.bottom / 2;
		t.left		= rect.right / 2;
		t.right		= rect.right / 2;
		t.top		= rect.bottom / 2;
		r1 = t;
		r2 = t;
		return 0;

	case WM_TIMER:	//Timer message that updates the screen and causes a redraw
		r1.bottom	+= r;
		r1.left		+= r;
		r1.right	+= r;
		r1.top		+= r;
		r2.bottom	+= r;
		r2.left		+= r;
		r2.right	+= r;
		r2.top		+= r;
		if  ( !lflag )
			InvalidateRect(hwnd, &rect, false);
		return 0;

	case WM_ERASEBKGND:
		if ( rflag )
			return 0;
		break;

	case WM_PAINT:
		if ( lflag )
			return 0;

		hdc = BeginPaint (hwnd, &ps);

		FillRect( hdc, &rect, RGB(256,256,256) );
		SelectObject( hdc, GetStockObject(HOLLOW_BRUSH) );		//Paint using a hollow brush
		Ellipse(hdc, r1.left, r1.top, r1.right, r1.bottom );	//Paint ellipse 1
		Ellipse(hdc, r2.left, r2.top, r2.right, r2.bottom );	//Paint ellipse 2
		ss.setString("SUMMER IS COMING SOON!");
		len = strlen(ss.getString());
		TextOut( hdc, 0, 0, ss.getString(), len );				//String output
		ss.setString("Press Left or Right Mouse Button to make selection");
		len = strlen(ss.getString());
		TextOut( hdc, 0, 20, ss.getString(), len);				//String output

		EndPaint ( hwnd, &ps );

		return 0;

	case WM_DESTROY:
		PostQuitMessage (0);
		return 0;

	}

	return DefWindowProc (hwnd, message, wParam, lParam);
}

//Timer function that sends a message to the window (Wanted to demonstrate a seperate callback function)
VOID CALLBACK Timer( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime ) {
	SendMessage(hwnd, WM_TIMER, 0, 0 );
}